package controller

import (
	"blog/dao"
	"github.com/julienschmidt/httprouter"
	"html/template"
	"net/http"
	"strconv"
)

/**

 */
func AdminTypeHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	guest := new(dao.GuestSession)
	//检查是否未登录
	if guest.Init(r).CheckIsLogin() == false {
		dao.RedirectPage(w, "Error", "未登录", "/admin/user/login", 503, 3)
		return
	}

	//渲染页面
	t, err := template.New("adminType.html").Funcs(dao.FuncMap).ParseFiles("templates/adminType.html", "templates/pub_inc.html", "templates/admin_inc.html")
	if err != nil {
		dao.LogError(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	t.Execute(w, nil)
}

/**

 */
/**
 * @api            		{GET} /admin/types/all 获取所有的分类
 * @apiName        		admin types all
 * @apiGroup       		admin/types
 * @apiVersion     		1.0.0
 * @apiDescription 		获取所有的分类
 * @apiPermission  		admin
 * @apiSampleRequest 	/admin/types/all
 * @apiParam {number} 	fid           	父级id
 */
func AdminTypeAllHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	guest := new(dao.GuestSession)
	//检查是否未登录
	if guest.Init(r).CheckIsLogin() == false {
		dao.SendResult(w, dao.ERR_NO_LOGIN.Code, dao.ERR_NO_LOGIN.Message, nil)
		return
	}

	types := dao.GetTypeList(0, -1)
	dao.SendResult(w, dao.ERR_NONE.Code, dao.ERR_NONE.Message, types)
}

/**
 * @api            		{POST} /admin/types/add 添加分类
 * @apiName        		admin types add
 * @apiGroup       		admin/types
 * @apiVersion     		1.0.0
 * @apiDescription 		添加分类
 * @apiPermission  		admin
 * @apiSampleRequest 	/admin/types/add
 * @apiParam {number} 	fid           	父级id
 * @apiParam {string} 	name          	名称
 * @apiParam {number} 	read_level    	阅读账号登陆
 * @apiParam {number} 	is_show       	是否显示 1是 0否
 */
func AdminTypeAddHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	guest := new(dao.GuestSession)
	//检查是否未登录
	if guest.Init(r).CheckIsLogin() == false {
		dao.SendResult(w, dao.ERR_NO_LOGIN.Code, dao.ERR_NO_LOGIN.Message, nil)
		return
	}

	postType := dao.TypeName{}

	//fid 父级ID
	fid, _ := strconv.ParseInt(r.PostFormValue("fid"), 10, 64)
	is_fid_ok, errMsg := postType.CheckAddFid(int(fid))
	if is_fid_ok == false {
		dao.SendResult(w, dao.ERR_POST_TYPE_FID_ERROR.Code, errMsg, nil)
		return
	}
	postType.Fid = int(fid)

	//name 名称
	name := r.PostFormValue("name")
	is_name_ok, errMsg := postType.CheckAddName(name, postType.Fid)
	if is_name_ok == false {
		dao.SendResult(w, dao.ERR_POST_TYPE_NAME_ERROR.Code, errMsg, nil)
		return
	}
	postType.Name = name

	//read_level
	read_level, _ := strconv.ParseUint(r.PostFormValue("read_level"), 10, 8)
	postType.ReadLevel = int(read_level)

	//is_show
	is_show, _ := strconv.ParseUint(r.PostFormValue("is_show"), 10, 8)
	if is_show == 0 {
		postType.IsShow = 0
	} else {
		postType.IsShow = 1
	}

	postType.FName = postType.GetTypeName(postType.Fid)

	err := postType.Add()
	if err != nil {
		dao.SendResult(w, dao.ERR_POST_TYPE_ADD_ERROR.Code, dao.ERR_POST_TYPE_ADD_ERROR.Message, postType)
		return
	}
	dao.SendResult(w, dao.ERR_NONE.Code, dao.ERR_NONE.Message, postType)
}

/**
 * @api            		{POST} /admin/types/update 修改分类
 * @apiName        		admin types update
 * @apiGroup       		admin/types
 * @apiVersion     		1.0.0
 * @apiDescription 		添加分类
 * @apiPermission  		admin
 * @apiSampleRequest 	/admin/types/update
 * @apiParam {number} 	id           	id
 * @apiParam {number} 	fid           	父级id
 * @apiParam {string} 	name          	名称
 * @apiParam {number} 	read_level    	阅读账号登陆
 * @apiParam {number} 	is_show       	是否显示 1是 0否
 */
func AdminTypeUpdateHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	guest := new(dao.GuestSession)
	//检查是否未登录
	if guest.Init(r).CheckIsLogin() == false {
		dao.SendResult(w, dao.ERR_NO_LOGIN.Code, dao.ERR_NO_LOGIN.Message, nil)
		return
	}

	id, _ := strconv.ParseInt(r.PostFormValue("id"), 10, 64)
	postType := dao.TypeName{}
	postType.Id = int(id)
	err := postType.Info()
	if err != nil {
		dao.SendResult(w, dao.ERR_POST_TYPE_NOT_FIND.Code, dao.ERR_POST_TYPE_NOT_FIND.Message, nil)
		return
	}

	//fid 父级ID
	fid, _ := strconv.ParseInt(r.PostFormValue("fid"), 10, 64)
	is_fid_ok, errMsg := postType.CheckAddFid(int(fid))
	if is_fid_ok == false {
		dao.SendResult(w, dao.ERR_POST_TYPE_FID_ERROR.Code, errMsg, nil)
		return
	}
	postType.Fid = int(fid)

	//name 名称
	name := r.PostFormValue("name")
	is_name_ok, errMsg := postType.CheckAddName(name, postType.Fid)
	if is_name_ok == false {
		dao.SendResult(w, dao.ERR_POST_TYPE_NAME_ERROR.Code, errMsg, nil)
		return
	}
	postType.Name = name

	//read_level
	read_level, _ := strconv.ParseUint(r.PostFormValue("read_level"), 10, 8)
	postType.ReadLevel = int(read_level)

	//is_show
	is_show, _ := strconv.ParseUint(r.PostFormValue("is_show"), 10, 8)
	if is_show == 0 {
		postType.IsShow = 0
	} else {
		postType.IsShow = 1
	}

	postType.FName = postType.GetTypeName(postType.Fid)

	err = postType.Update()
	if err != nil {
		dao.SendResult(w, dao.ERR_POST_TYPE_UPDATE.Code, dao.ERR_POST_TYPE_UPDATE.Message, nil)
		return
	}
	dao.SendResult(w, dao.ERR_NONE.Code, dao.ERR_NONE.Message, postType)
}
